var urlLogin = "/Member/login";
var LOGIN_SUCCESS = 1;
$(document).ready(function() {
	 $(document).on('click', '#signin', function(e){
	 	e.preventDefault();
	 	
	 	var email = $( "#memberLogin" ).find("#email").val();
	 	var password = $( "#memberLogin" ).find("#password").val();
	 	
	 	var data = {"data[User][email]":email, "data[User][password]":password};
	 	if(validateLogin(email,password)){
	 		submitForm(data);	
	 	}
	 	return false;
	 });
});

function submitForm(dataForm){
	$.ajax({
        url : urlLogin,
        type : "POST",
        data : dataForm,
        dataType : "json",
        beforeSend: function(){
			//console.log("beforeSend");
		},
		success: function(data1){
			if(data1.success == LOGIN_SUCCESS){
				window.location.href="/quan-ly-user";
			}
			if(data1.success == 2){
				$("#modal_login_error").html(data1.msg);
			}
		},
		error: function(error){
			$("#modal_login_error").html("Vui lòng thử lại");
		}
    });
}
function validateEmail(email) { 
	if(email == ""){
		return false;
	}
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function validateLogin(email, pass){
	if (!validateEmail(email)){
		$("#modal_login_error").html("Vui lòng nhập chính xác email");
		return false;
	}
	if(pass == ""){
		$("#modal_login_error").html("Chưa nhập mật khẩu");
		return false;
	}
	return true;
}
